package org.jeecg.modules.basic.unit.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.basic.materialtype.entity.BasicMaterialTreeVO;
import org.jeecg.modules.basic.unit.entity.BasicUnit;
import org.jeecg.modules.basic.unit.service.IBasicUnitService;
import org.jeecg.modules.business.job.service.SyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 计量单位表
 * @Author: jeecg-boot
 * @Date: 2023-07-26
 * @Version: V1.0
 */
@Api(tags = "计量单位表")
@RestController
@RequestMapping("/unit")
@Slf4j
public class BasicUnitController extends JeecgController<BasicUnit, IBasicUnitService> {
    @Autowired
    private IBasicUnitService basicUnitService;

    @Autowired
    private SyncService syncService;

    /**
     * 查询单位分类列表
     *
     * @return
     */
    @ApiOperation(value = "计量单位表-查询单位分类", notes = "计量单位表-查询单位分类")
    @GetMapping(value = "/parentList")
    public Result<List<BasicUnit>> parentList(String name) {
        List<BasicUnit> list = basicUnitService.listParent(name);
        return Result.OK(list);
    }

    /**
     * 分页查询计量单位列表
     *
     * @return
     */
    @ApiOperation(value = "计量单位表-查询计量单位", notes = "计量单位表-查询计量单位")
    @GetMapping(value = "/childList")
    public Result<Page<BasicUnit>> childList(BasicUnit unit,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {
        Result<Page<BasicUnit>> result = new Result<Page<BasicUnit>>();
        Page<BasicUnit> pageList = new Page<BasicUnit>(pageNo, pageSize);
        pageList = basicUnitService.pageChild(pageList, unit);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param unit
     * @return
     */
    @AutoLog(value = "计量单位表-添加")
    @ApiOperation(value = "计量单位表-添加", notes = "计量单位表-添加")
    @RequiresPermissions("basic:unit:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody BasicUnit unit) {
        unit.setUsed(0);
        basicUnitService.save(unit);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param unit
     * @return
     */
    @AutoLog(value = "计量单位表-编辑")
    @ApiOperation(value = "计量单位表-编辑", notes = "计量单位表-编辑")
    @RequiresPermissions("basic:unit:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody BasicUnit unit) {
        basicUnitService.updateById(unit);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "计量单位表-通过id删除")
    @ApiOperation(value = "计量单位表-通过id删除", notes = "计量单位表-通过id删除")
    @RequiresPermissions("basic:unit:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        basicUnitService.deleteUnitById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "计量单位表-批量删除")
    @ApiOperation(value = "计量单位表-批量删除", notes = "计量单位表-批量删除")
    @RequiresPermissions("basic:unit:delete")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        //先删除选中的
        List<String> list = Arrays.asList(ids.split(","));
        basicUnitService.removeByIds(list);
        //如果选中单位下面有子单位,则删除子单位
        List<BasicUnit> unitList = basicUnitService.list(new LambdaQueryWrapper<BasicUnit>()
                .in(BasicUnit::getPid, list)
        );
        if (CollectionUtil.isNotEmpty(unitList)) {
            basicUnitService.removeByIds(unitList.stream().map(BasicUnit::getId).collect(Collectors.toList()));
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "计量单位表-通过id查询", notes = "计量单位表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BasicUnit> queryById(@RequestParam(name = "id", required = true) String id) {
        BasicUnit basicUnit = basicUnitService.getById(id);
        if (basicUnit == null) {
            return Result.error("未找到对应数据");
        }
        basicUnit.setStatus_dictText("1".equals(basicUnit.getStatus()) ? "启用" : "0".equals(basicUnit.getStatus()) ? "禁用" : "");
        return Result.OK(basicUnit);
    }

    /**
     * 查询树形结构所有数据
     *
     * @return
     */
    @ApiOperation(value = "计量单位表-查询树形结构所有数据", notes = "计量单位表-查询树形结构所有数据")
    @GetMapping("/getTree")
    public Result<List<BasicMaterialTreeVO>> getTree() {
        return Result.OK(basicUnitService.listTree());
    }

    /**
     * 导出excel
     *
     * @param request
     * @param basicUnit
     */
    @ApiOperation(value = "计量单位表-导出", notes = "计量单位表-导出")
    @RequiresPermissions("basic:unit:export")
    @GetMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicUnit basicUnit) {
        return super.exportXls(request, basicUnit, BasicUnit.class, "计量单位");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "计量单位表-导入", notes = "计量单位表-导入")
    @RequiresPermissions("basic:unit:import")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {
        return basicUnitService.importExcel(request);
    }

    /**
     * 分页查询所有计量单位
     *
     * @return
     */
    @ApiOperation(value = "计量单位表-分页查询所有计量单位", notes = "计量单位表-分页查询所有计量单位")
    @GetMapping(value = "/allChild")
    public Result<Page<BasicUnit>> allChild(BasicUnit unit,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            HttpServletRequest req) {
        Result<Page<BasicUnit>> result = new Result<Page<BasicUnit>>();
        Page<BasicUnit> pageList = new Page<BasicUnit>(pageNo, pageSize);
        pageList = basicUnitService.allChild(pageList, unit);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 计量单位表-同步sap
     *
     * @return
     */
    @ApiOperation(value = "计量单位表-同步sap", notes = "计量单位表-同步sap")
    @GetMapping("/sync-unit")
    public Result syncUnit() {
        syncService.syncUnit();
        return Result.OK("同步单位成功！");
    }
}
